<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(nums) {
      var build = function (nums, lo, hi) {
        if (lo > hi) return null
        // 找到区间最大值与索引
        let maxVal = -Infinity, index = -1
        for (let i = lo; i <= hi; i++) {
          if (maxVal < nums[i]) {
            maxVal = nums[i]
            index = i
          }
        }
        // 构造根节点
        let root = new TreeNode(maxVal)
        // 构造左右子树
        root.left = build(nums, lo, index - 1)
        root.right = build(nums, index + 1, hi)
        return root
      }
      return build(nums, 0, nums.length - 1)
    };
    console.log(arr([3, 2, 1, 6, 0, 5]));
  </script>
</body>

</html>